/** # component  : Carousel;
	# date       : 2012.02.17
	# author     : UI development team  part of lab.
**/

Fsz.Component.add("Carousel", Fsz.Class({
 /* Constructor*/
initialize : function (info){
	this.callBackArr = [];
	
	this.info  = info; /* carousel element information */
	
	this._portable = (this.info.portable != undefined && this.info.portable) ? true : false;
	
	this._anipoint = 0; /* animation start point */	
	this._index = 0;
	this._way = null;
	this._repeat = (this.info.paging != undefined && this.info.repeat) ? false : true;
	this._repeat = (this.info.paging == undefined) ? this.info.repeat : this._repeat;
	this._root = $(this.info.id);
	this._mask = this.getMask();
	this._container = this.getContainer();
	this._nodes = this.getNode();
	this._max = this.setMax();
	this._term = this.setTerm();
	this._speed = this.setSpeed();
	this._tick = this.getTick();
	
	this._nodeTotal = this._nodes.length;
	this._controlSet = this.getController();
	
	this.timer1 = null;
	this.timer2 = null;
	
	this._saveNode = [];
	
	
	this.setRoot();		
	this.setController();
	this.checkControlEnable();
	this.checkPagingControl();

},
getContainer : function(){
	var root = $(this.info.id), items = root.$$("*"), i;
	for(i = 0; i < items.length; i++){
		if(items[i].className.indexOf(this.info.cls) > -1){
			return items[i];
		}
	}
},
getMask : function(){
	if(this._container != undefined){
		return $(this._container).parent();
	}
},
getNode : function(){
	var item = this._container.$$("*"), nodes = [], i;
	for(i = 0;i< item.length; i++){
		if(item[i].parent() === this._container){
			nodes.push(item[i]);
		}
	}
	if(!this._repeat){
		nodes[0].addClassName("first");
		nodes[nodes.length-1].addClassName("last");
	}
	return nodes;
},
getTick : function(){
	var tick = parseInt(this._term / this._speed);
	return tick;
},
getController : function(){
	var element = this.info.nav_Tag.slice(0,this.info.nav_Tag.indexOf("."));
	var cls = this.info.nav_Tag.slice(this.info.nav_Tag.indexOf(".") + 1,this.info.nav_Tag.length);
	
	var controlSet = [], temp = [];
	temp = this._root.$$(element);
	if(temp.length > 2){
		for(var i = 0;i<temp.length;i++){
			if(temp[i].className.indexOf(cls) > -1){
				controlSet.push(temp[i]);
			}
		}
	}
	else{
		controlSet = temp;
	}
	return controlSet;
},
setRoot : function(){
	this._container.style.width = parseInt(this._nodes[0].clientWidth) * this._nodeTotal + "px";
	
	if(this._portable){
		this._container.style.top = 0;
	}else{
		this._container.style.left = 0;
	}
},
removeSpace : function(){
	var cnt = 0;
	while(cnt < this._container.$$("li").length){
		if(this._container.$$("li")[cnt].nodeType != 1){
			this._container.removeChild(this._container.$$("li")[cnt]);
			cnt++;
		}
	}
},
setController : function(){
	//Event.observe(this._controlSet[0],"click",this.moveLeft.bind(this));
	//Event.observe(this._controlSet[1],"click",this.moveRight.bind(this));
	this._controlSet[0].onclick = this.moveLeft.bind(this);
	this._controlSet[1].onclick = this.moveRight.bind(this);
},
setMax : function(){	
	if(this.info.onecell){
		return this._nodes.length;
	}else{
		if(this._nodes.length % this.info.vnc !== 0){
			return parseInt(this._nodes.length / this.info.vnc);
		}else{
			return parseInt(this._nodes.length / this.info.vnc - 1);
		}
	}
},
setTerm : function(){
	var wid;
	if(this.info.onecell){
		wid = parseInt(this._nodes[0].clientWidth);
	}else{
		wid = parseInt(this._nodes[0].clientWidth * this.info.vnc);
	}
	if(this._portable){
		wid = (this.info.onecell) ? parseInt(this._nodes[0].clientHeight) : parseInt(this._nodes[0].clientHeight * this.info.vnc);
	}
	return wid;
},
setSpeed : function(){
	if(this.info.speed < 10){
		return 10;
	}else if(this.info.speed > 60){
		return 60;
	}else{
		return this.info.speed;
	}
},
checkControlEnable : function(){
	
	var cnt = 0, ind = 0;
	while(cnt < this.info.vnc){
		if(this._container.$$("li")[ind].nodeType == 1){
			cnt++;
		}
		ind++;
	}
	
	//if(this._container.$$("li")[0].nodeType == 1){
	if(this._container.$$("li")[0].className.indexOf("first") >= 0){
		this._controlSet[0].addClassName("disable");
	}else{
		this._controlSet[0].removeClassName("disable");
	}
	/*}else{
		if(this._container.$$("li")[0].nextSibling.className.indexOf("first") >= 0){
			this._controlSet[0].addClassName("disable");
		}else{
			this._controlSet[0].removeClassName("disable");
		}
	}*/
	
	if(this._container.$$("li")[--ind].className.indexOf("last") >= 0){
		this._controlSet[1].addClassName("disable");
	}else{
		this._controlSet[1].removeClassName("disable");
	}
	
},
checkPagingControl : function(){

	if(this.info.paging != undefined){
	
		if(this.info.paging.indexOf("number") < 0){
			this._numberType = false;
		}else{
			this._numberType = true;
		}
				
		this._pagingRoot = $(this.info.paging)
		this._hasPaging = true;
		if(!this._numberType){
			this.makePagingControl();
		}else{
			this.makePagingNumber();
		}
		
	}else{
		this._hasPaging = false;
	}
},
makePagingControl : function(){
	
	var i,j, tempNode = this._pagingRoot.childNodes, pageController = [];
	
	for(i = 0; i < tempNode.length; i++){
		if(tempNode[i].nodeType == 1){
			pageController.push(tempNode[i]);
		}
	}
	
	this.onPageClass = pageController[0].className;
	
	var self = this;
	this._navNode = (function(){
		var i, arr = [];
		for(i = 0; i <= self._max;i++){
			if(i == self._index){
				arr.push(pageController[0].cloneNode(false));
			}else{
				arr.push(pageController[1].cloneNode(false));
			}
			arr[i].innerHTML = (i+1);
		};
		return arr;
	})();
	

	this._pagingRoot.innerHTML = "";
	
	for(j = 0;j< this._navNode.length;j++){
		this._pagingRoot.appendChild(this._navNode[j]);
	}
	
	
},
makePagingNumber : function(){
	var i, tempNode = this._pagingRoot.childNodes, pg_els = [], child = this._pagingRoot;
	
	while(child.firstChild != undefined){
		if(child.firstChild.nodeType == 1) pg_els.push(child.firstChild.cloneNode(true));
		child.removeChild(child.firstChild);
	}

		
	if(pg_els.length > 1){
		pg_els[0].innerHTML = this._index + 1;
		pg_els[1].innerHTML = this._max + 1;
	}else{
		pg_els[0].innerHTML = this._index + 1;
		pg_els[1] = document.createTextNode(this._max + 1);
	}
	
	child.appendChild(pg_els[0]);
	child.appendChild(document.createTextNode(" / "));
	child.appendChild(pg_els[1]);
	
	this._pagingIndex = pg_els[0];
	
},
setPagingControl : function(){
	if(!this._numberType){
		var j;
		
		for(j = 0;j< this._navNode.length;j++){
			var now = this._navNode[j];
			if(now.className.indexOf(this.onPageClass) >= 0){
				now.className = now.className.replace(this.onPageClass,"");
			}else if(j == this._index){
				now.className = now.className + this.onPageClass;
			}
		}
	
	}else{
		this._pagingIndex.innerHTML = this._index+1;
	}
	
},
moveLeft : function(){
	
	
	var first = this._container.$$("li")[0].className.indexOf("first") >= 0 ? false : true;
	
	
	if(first){
		this.appendNodeLeft();
		this._index--;
		if(this.info.animate){
			this.animate("prev");
		}else{
			this.setPosition("prev");
		}
		if(this._hasPaging) this.setPagingControl();
	}
	
	
	if(this.info.callBack != undefined){
		this.callBackArr[this.info.callBack]();
	}

},
moveRight : function(){
	var cnt = 0;
	var lastPoint = this.info.vnc-1;
	while(cnt < this.info.vnc){
		if(this._container.$$("li")[cnt].nodeType != 1){
			lastPoint = (this.info.vnc * 2) - 1
		}
		cnt++;
	}
	var last = this._container.$$("li")[lastPoint].className.indexOf("last") >= 0 ? false : true;
	if(last){
		this._index++;
		this.appendNodeRight();
		
		if(this.info.animate){
			this.animate("next");
		}else{
			this.setPosition("next");
		}
		if(this._hasPaging) this.setPagingControl();
	}

	if(this.info.callBack != undefined){
		this.callBackArr[this.info.callBack]();
	}
},
animate : function(way){
	this._way = way;
	this.timer = setTimeout(this.move.bind(this),10);
},
move: function(){
	var self = this;
	var movePoint = this._container.style;
	
	if(this._anipoint < (this._term - this._tick)){
		
		if(this._way == "prev"){
			if(this._portable){
				movePoint.top = parseInt(movePoint.top) + this._tick + "px";
			}else{
				movePoint.left = parseInt(movePoint.left) + this._tick + "px";				
			}
		}else{
			if(this._portable){
				movePoint.top = parseInt(movePoint.top) - this._tick + "px";
			}else{
				movePoint.left = parseInt(movePoint.left) - this._tick + "px";
			}
		}
		this._anipoint = this._anipoint + this._tick;
		this.timer = setTimeout(this.move.bind(this,self),10);
	}
	else{
		if(!this._portable){
			var over = parseInt(movePoint.left) % this._term;
		}else{
			var over = parseInt(movePoint.top) % this._term;
		}
		
		if(this._way == "prev"){
			if(over !== 0){
				if(this._portable){
					movePoint.top =  parseInt(movePoint.top) + (this._term-(this._term + over)) + "px";
				}else{
					movePoint.left =  parseInt(movePoint.left) + (this._term-(this._term + over)) + "px";
				}
			}
			this.removeNodeRight();
		}else{
			if(over !== 0){
				if(this._portable){
					movePoint.top =  parseInt(movePoint.top) - (this._term + over) + "px";
				}else{
					movePoint.left =  parseInt(movePoint.left) - (this._term + over) + "px";
				}
			}
			this.removeNodeLeft();
		}
		
		this._anipoint = 0;
		clearTimeout(this.timer);
	}
	
},
setPosition : function(way){
	var fixPoint = this._container.style;
	if(way === "prev"){
		if(this._portable){
			fixPoint.top = parseInt(fixPoint.top) + this._term + "px";
		}else{
			fixPoint.left = parseInt(fixPoint.left) + this._term + "px";
		}
		this.removeNodeRight();
	}else{
		if(this._portable){
			fixPoint.top = parseInt(fixPoint.top) - this._term + "px";
		}else{
			fixPoint.left = parseInt(fixPoint.left) - this._term + "px";
		}
		this.removeNodeLeft();
	}
	
},
copyNodes : function(){
	var i, clone = [];
	var self = this;
	var node = (function(){
		var result = [];
		var list = self._container.$$("li");
		for(var i = 0; i < list.length; i++){
			if(list[i].nodeType == 1){
				result.push(list[i]);
			}
		}
		return result;
	})();
	for(i = 0; i < node.length; i++){
		clone.push(node[i].cloneNode(true));
	}
	return clone;
},
appendNodeLeft : function(){
	var i, sliceNode = [];
	var cloneList = this.copyNodes();
	if(this.info.onecell){
		var count = 1;
	}else{
		var count = this.info.vnc;
	}
	for(i = cloneList.length-1; i > cloneList.length - (count + 1); i--){
		sliceNode.push(cloneList[i]);
	}
	
	for(i = 0; i< sliceNode.length; i++){
		this._container.insertBefore(sliceNode[i],this._container.$$("li")[0]);
	}
	
	var lis =this._container.$$("li"),
		lastIndex = lis.length - 1;
		
	lis[lastIndex].style.display = "none";
	
	
	this._saveNode = sliceNode;
	if(this._portable){
		this._container.style.top = parseInt(this._container.style.top) - this._term + "px";
	}else{
		this._container.style.left = parseInt(this._container.style.left) - this._term + "px";
	}
	
},
removeNodeLeft : function(){
	if(this.info.onecell){
		var count = 1;
	}else{
		var count = this.info.vnc;
	}
	cnt = 0;
	while(cnt < count){
		//if(this._container.$$("li")[0].nodeType == 1){
			this._container.removeChild(this._container.$$("li")[0]);
			cnt++;
		/*}else{
			this._container.removeChild(this._container.$$("li")[0]);
		}*/
		
	}
	if(this._portable){
		this._container.style.top = parseInt(this._container.style.top) + this._term + "px";
	}else{
		this._container.style.left = parseInt(this._container.style.left) + this._term + "px";
	}
	
	if(!this._repeat){
		this.checkControlEnable();
	}
},
appendNodeRight : function(){
	var i, sliceNode = [];
	var cloneList = this.copyNodes();
	if(this.info.onecell){
		var count = 1;
	}else{
		var count = this.info.vnc;
	}
	for(i = 0; i < count; i++){
		sliceNode.push(cloneList[i]);
	}
	
	
	for(i = 0; i < sliceNode.length; i++){
		this._container.appendChild(sliceNode[i]);
	}
	this._saveNode = sliceNode;
},
removeNodeRight : function(){
	if(this.info.onecell){
		var count = 1;
	}else{
		var count = this.info.vnc;
	}
	cnt = 0;
	while(cnt < count){
		if(this._container.lastChild.nodeType == 1){
			this._container.removeChild(this._container.lastChild);
			cnt++;
		}else{
			this._container.removeChild(this._container.lastChild);
		}
		
	}
	
	if(!this._repeat){
		this.checkControlEnable();
	}
},
addCallBack : function(name,fc){
		this.callBackArr[name]  = fc;
}
}));